package tr.edu.metu.is.mega.server.business;

import java.util.List;

import tr.edu.metu.is.mega.common.dto.UserDTO;
import tr.edu.metu.is.mega.server.core.PagedQueryParams;
import tr.edu.metu.is.mega.server.dao.UserDAO;
import tr.edu.metu.is.mega.server.entity.Systemuser;

/**
 * Class to manage users and authentication features.
 */
public class UserManager {

    private final UserDAO dao;

    private final UserDTOHelper helper;

    /**
     * Default constructor.
     */
    public UserManager() {
        dao = new UserDAO();
        helper = new UserDTOHelper();
    }

    /**
     * Logs in a user.
     * 
     * @param userid user id
     * @param password password
     * @return logged in user
     */
    public UserDTO login(String userid, String password) {
        Systemuser user = new Systemuser();
        user.setUsername(userid);
        user.setPassword(password);
        List<Systemuser> list = dao.findByExample(user, false);
        if (list.size() == 1) {
            return helper.convertToDTO(list.get(0));
        }
        return null;
    }

    /**
     * Returns user count matching given criteria.
     * 
     * @param filter filter instance
     * @return user count
     */
    public Long getUserCount(UserDTO filter) {
        Systemuser entity = new Systemuser();
        helper.convertToEntity(filter, entity);
        return dao.getCountByExample(entity, true);
    }

    /**
     * Queries and returns users matching criteria.
     * 
     * @param params paging parameters
     * @param filter filter instance
     * @return list of users
     */
    public List<UserDTO> getUsers(PagedQueryParams params, UserDTO filter) {
        Systemuser entity = new Systemuser();
        helper.convertToEntity(filter, entity);
        List<Systemuser> entities = dao.findByExample(entity, false, params);
        return helper.toDTO(entities);
    }

    /**
     * Updates a user record.
     * 
     * @param user user instance
     * @return updated user instance
     */
    public UserDTO updateUser(UserDTO user) {
        Systemuser entity = dao.findById(user.getPkid());
        helper.convertToEntity(user, entity);
        Systemuser update = dao.update(entity);
        return helper.convertToDTO(update);
    }

    /**
     * Creates a new user record.
     * 
     * @param user user instance
     * @return created user instance
     */
    public UserDTO createUser(UserDTO user) {
        Systemuser entity = new Systemuser();
        helper.convertToEntity(user, entity);
        Systemuser create = dao.create(entity);
        UserDTO convertToDTO = helper.convertToDTO(create);
        MailService.welcomeUser(convertToDTO);
        return convertToDTO;
    }

    /**
     * Deletes a user record
     * 
     * @param user user to be deleted
     */
    public void deleteUser(UserDTO user) {
        dao.deleteById(user.getPkid());
    }

}
